﻿
// FindSCUDlg.cpp: 实现文件
//

#include "pch.h"
#include "framework.h"
#include "FindSCU.h"
#include "FindSCUDlg.h"
#include "afxdialogex.h"


#ifdef _DEBUG
#define new DEBUG_NEW
#endif

#define UM_ADD_LOG      WM_USER + 10

// 用于应用程序“关于”菜单项的 CAboutDlg 对话框

class CAboutDlg : public CDialogEx
{
public:
	CAboutDlg();

// 对话框数据
#ifdef AFX_DESIGN_TIME
	enum { IDD = IDD_ABOUTBOX };
#endif

	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV 支持

// 实现
protected:
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialogEx(IDD_ABOUTBOX)
{
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)
END_MESSAGE_MAP()


// CFindSCUDlg 对话框



CFindSCUDlg::CFindSCUDlg(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_FINDSCU_DIALOG, pParent)
	, m_patId(_T(""))
	, m_patName(_T(""))
	, m_dtBegin(COleDateTime::GetCurrentTime())
	, m_dtEnd(COleDateTime::GetCurrentTime())
	, m_localAET(_T("xg-findscu"))
	, m_remoteAET(_T("xg-wlscp"))
	, m_port(3000)
	, m_serverIP(_T("127.0.0.1"))
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	m_brBlack.CreateSolidBrush(RGB(0, 0, 0));
}

void CFindSCUDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_LIST_WORK, m_wlist);
	DDX_Control(pDX, IDC_EDIT_MSCU_LOG, m_log);
	DDX_Text(pDX, IDC_EDIT_FINDSCU_PATID, m_patId);
	DDX_Text(pDX, IDC_EDIT_FINDSCU_PATNAME, m_patName);
	DDX_Control(pDX, IDC_COMBO_FINDSCU_SEX, m_cmbSex);
	DDX_DateTimeCtrl(pDX, IDC_DATETIMEPICKER_FINDSCU_BEGIN, m_dtBegin);
	DDX_DateTimeCtrl(pDX, IDC_DATETIMEPICKER_FINDSCU_END, m_dtEnd);
	DDX_Text(pDX, IDC_EDIT_FINDSCU_LAET, m_localAET);
	DDX_Text(pDX, IDC_EDIT_FINDSCU_RAET, m_remoteAET);
	DDX_Text(pDX, IDC_EDIT_FINDSCU_PORT, m_port);
	DDX_Text(pDX, IDC_EDIT_FINDSCU_RIP, m_serverIP);
	DDX_Control(pDX, IDC_COMBO_MSCU_LL, m_cmbLL);
	DDX_Control(pDX, IDC_DATETIMEPICKER_FINDSCU_BEGIN, m_ctldtBegin);
	DDX_Control(pDX, IDC_DATETIMEPICKER_FINDSCU_END, m_ctldtEnd);
}

BEGIN_MESSAGE_MAP(CFindSCUDlg, CDialogEx)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON_MSCU_ECHO, &CFindSCUDlg::OnBnClickedButtonMscuEcho)
	ON_BN_CLICKED(IDC_BUTTON_MSCU_QRY, &CFindSCUDlg::OnBnClickedButtonMscuQry)
	ON_BN_CLICKED(IDC_BUTTON_MSCU_CLRLIST, &CFindSCUDlg::OnBnClickedButtonMscuClrlist)
	ON_BN_CLICKED(IDC_BUTTON_MSCU_CLRLOG, &CFindSCUDlg::OnBnClickedButtonMscuClrlog)
	ON_MESSAGE(UM_ADD_LOG, &CFindSCUDlg::OnFindSCULog)
	ON_WM_CTLCOLOR()
	ON_NOTIFY(DTN_DATETIMECHANGE, IDC_DATETIMEPICKER_FINDSCU_BEGIN, &CFindSCUDlg::OnDatetimechangeDatetimepickerFindscuBegin)
	ON_NOTIFY(DTN_DATETIMECHANGE, IDC_DATETIMEPICKER_FINDSCU_END, &CFindSCUDlg::OnDatetimechangeDatetimepickerFindscuEnd)
END_MESSAGE_MAP()


// CFindSCUDlg 消息处理程序

BOOL CFindSCUDlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// 将“关于...”菜单项添加到系统菜单中。

	// IDM_ABOUTBOX 必须在系统命令范围内。
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != nullptr)
	{
		BOOL bNameValid;
		CString strAboutMenu;
		bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
		ASSERT(bNameValid);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// 设置此对话框的图标。  当应用程序主窗口不是对话框时，框架将自动
	//  执行此操作
	SetIcon(m_hIcon, TRUE);			// 设置大图标
	SetIcon(m_hIcon, FALSE);		// 设置小图标

	// TODO: 在此添加额外的初始化代码
	m_cmbLL.AddString(_T("调试"));
	m_cmbLL.AddString(_T("通知"));
	m_cmbLL.AddString(_T("警告"));
	m_cmbLL.AddString(_T("错误"));
	m_cmbLL.SetCurSel(1);

	m_cmbSex.AddString(_T("男"));
	m_cmbSex.AddString(_T("女"));
	m_cmbSex.AddString(_T("未知"));
	m_cmbSex.SetCurSel(0);

	int nCol = 0;
	m_wlist.SetExtendedStyle(LVS_EX_GRIDLINES | LVS_EX_FULLROWSELECT | LVS_EX_FLATSB);
	m_wlist.InsertColumn(nCol++, _T("检查编号"), LVCFMT_LEFT, 80);  //0
	m_wlist.InsertColumn(nCol++, _T("姓名"), LVCFMT_LEFT, 65);       //1
	m_wlist.InsertColumn(nCol++, _T("性别"), LVCFMT_LEFT, 45);       //2
	m_wlist.InsertColumn(nCol++, _T("出生日期"), LVCFMT_LEFT, 75);   //3
	m_wlist.InsertColumn(nCol++, _T("设备"), LVCFMT_LEFT, 45);       //4
	m_wlist.InsertColumn(nCol++, _T("患者编号"), LVCFMT_LEFT, 75);   //5
	m_wlist.InsertColumn(nCol++, _T("检查描述"), LVCFMT_LEFT, 110);  //6
	m_wlist.InsertColumn(nCol++, _T("申请医生"), LVCFMT_LEFT, 75);   //7
	m_wlist.InsertColumn(nCol++, _T("字符集"), LVCFMT_LEFT, 60);    //8

	return TRUE;  // 除非将焦点设置到控件，否则返回 TRUE
}

void CFindSCUDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialogEx::OnSysCommand(nID, lParam);
	}
}

// 如果向对话框添加最小化按钮，则需要下面的代码
//  来绘制该图标。  对于使用文档/视图模型的 MFC 应用程序，
//  这将由框架自动完成。

void CFindSCUDlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // 用于绘制的设备上下文

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// 使图标在工作区矩形中居中
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// 绘制图标
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialogEx::OnPaint();
	}
}

//当用户拖动最小化窗口时系统调用此函数取得光标
//显示。
HCURSOR CFindSCUDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}



void CFindSCUDlg::OnBnClickedButtonMscuEcho()
{
	UpdateData();

	int ll = m_cmbLL.GetCurSel();
	FindItem fi;
	SCUParam param;
	param.remoteAET = m_remoteAET;
	param.serverIP = m_serverIP;
	param.hLogWnd = GetSafeHwnd();
	param.loglevel = ll;
	param.localAET = m_localAET;
	param.port = m_port;
	m_FindSCU.SetParameter(param);
	m_FindSCU.Echo();
}

void CFindSCUDlg::OnBnClickedButtonMscuQry()
{
	UpdateData();

	int ll = m_cmbLL.GetCurSel();
	int sex = m_cmbSex.GetCurSel();

	CString strSex;
	m_cmbSex.GetWindowText(strSex);


	FindItem qry;
	qry.patName = m_patName;
	qry.patId = m_patId;

	if (strSex == _T("男"))
		qry.patSex = _T("M");
	else if (strSex == _T("女"))
		qry.patSex = _T("F");
	else if (strSex == _T("未知"))
		qry.patSex = _T("O");
	else qry.patSex = _T("");

	//if (sex == 0)
	//	qry.patSex = _T("M");
	//else if (sex == 1)
	//	qry.patSex = _T("F");
	//else
	//	qry.patSex = _T("O");

	CString strBegin, strEnd;
	strBegin = m_dtBegin.Format(_T("%Y%m%d"));
	qry.startDate = strBegin;

	strEnd = m_dtEnd.Format(_T("%Y%m%d"));
	qry.endDate = strEnd;

	SCUParam param;
	param.remoteAET = m_remoteAET;
	param.serverIP = m_serverIP;
	param.hLogWnd = GetSafeHwnd();
	param.loglevel = ll;
	param.localAET = m_localAET;
	param.port = m_port;
	m_FindSCU.SetParameter(param);

	std::vector<FindItem> vec;
	m_FindSCU.WlFind(qry, vec);

	m_wlist.DeleteAllItems();
	int row = 0;

	for (int i = 0; i < vec.size(); i++)
	{
		FindItem item = vec.at(i);
		int col = 0;
		m_wlist.InsertItem(row, _T(""));

		CString tmp;
		tmp = item.accessNumber.c_str();
		m_wlist.SetItemText(row, col++, tmp);

		tmp = item.patName.c_str();
		//tmp = tmp.Left(2) + "**";
		m_wlist.SetItemText(row, col++, tmp);

		tmp = item.patSex.c_str();
		m_wlist.SetItemText(row, col++, tmp);

		tmp = item.patBirth.c_str();
		m_wlist.SetItemText(row, col++, tmp);

		tmp = item.modality.c_str();
		m_wlist.SetItemText(row, col++, tmp);

		tmp = item.patId.c_str();
		m_wlist.SetItemText(row, col++, tmp);

		tmp = item.studyPart.c_str();
		m_wlist.SetItemText(row, col++, tmp);

		tmp = item.physician.c_str();
		m_wlist.SetItemText(row, col++, tmp);

		tmp = item.charset.c_str();
		m_wlist.SetItemText(row, col++, tmp);

		row++;
	}

}

void CFindSCUDlg::OnBnClickedButtonMscuClrlist()
{
	m_wlist.DeleteAllItems();
}

void CFindSCUDlg::OnBnClickedButtonMscuClrlog()
{
	m_log.SetSel(0, -1);
	m_log.ReplaceSel(_T(""), FALSE);
}

LRESULT CFindSCUDlg::OnFindSCULog(WPARAM wParam, LPARAM lParam)
{
	CString logStr = (LPCTSTR)wParam;

	if (m_log.m_hWnd && IsWindow(m_log.m_hWnd))
	{
		int nLmt = m_log.GetLimitText();
		int nTextLength = m_log.GetWindowTextLength();
		if ((int)(nTextLength + logStr.GetLength()) >= nLmt)
		{
			int nPos = m_log.LineIndex(100);
			m_log.SetSel(0, nPos);
			m_log.ReplaceSel(_T(""), FALSE);
		}

		m_log.SetSel(nTextLength, nTextLength);
		m_log.ReplaceSel(logStr);
	}

	return 0;
}

HBRUSH CFindSCUDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
	HBRUSH hbr = CDialogEx::OnCtlColor(pDC, pWnd, nCtlColor);

	// TODO:  在此更改 DC 的任何特性

	if (pWnd && pWnd->m_hWnd == m_log.GetSafeHwnd())
	{
		//pDC->SetBkMode(TRANSPARENT);
		pDC->SetBkColor(0);
		pDC->SetTextColor(RGB(192, 192, 192));
		return m_brBlack;
	}

	// TODO:  如果默认的不是所需画笔，则返回另一个画笔
	return hbr;
}

void CFindSCUDlg::OnDatetimechangeDatetimepickerFindscuBegin(NMHDR *pNMHDR, LRESULT *pResult)
{
	LPNMDATETIMECHANGE pDTChange = reinterpret_cast<LPNMDATETIMECHANGE>(pNMHDR);
	// TODO: 在此添加控件通知处理程序代码

	static BOOL bOK = TRUE;
#if 1//判断是否有CMonthCalCtrl，若有会发送两次Change,截第二次即可
	CDateTimeCtrl* pCtrl = (CDateTimeCtrl*)GetDlgItem(IDC_DATETIMEPICKER_FINDSCU_BEGIN);
	ASSERT(pCtrl != NULL);
	CMonthCalCtrl* pMoCalCtrl = pCtrl->GetMonthCalCtrl();
	if (pMoCalCtrl != NULL)
		bOK = !bOK;
#endif

	if (bOK)
	{
		m_ctldtEnd.GetTime(m_dtEnd);
		COleDateTime newDate = pDTChange->st;
		if (newDate > m_dtEnd)
		{
			MessageBox(_T("开始时间不能大于结束时间"));
			m_ctldtBegin.SetTime(m_dtBegin);
		}

		UpdateData();
	}

	*pResult = 0;
}

void CFindSCUDlg::OnDatetimechangeDatetimepickerFindscuEnd(NMHDR *pNMHDR, LRESULT *pResult)
{
	LPNMDATETIMECHANGE pDTChange = reinterpret_cast<LPNMDATETIMECHANGE>(pNMHDR);
	// TODO: 在此添加控件通知处理程序代码

	static BOOL bOK = TRUE;
#if 1//判断是否有CMonthCalCtrl，若有会发送两次Change,截第二次即可
	CDateTimeCtrl* pCtrl = (CDateTimeCtrl*)GetDlgItem(IDC_DATETIMEPICKER_FINDSCU_END);
	ASSERT(pCtrl != NULL);
	CMonthCalCtrl* pMoCalCtrl = pCtrl->GetMonthCalCtrl();
	if (pMoCalCtrl != NULL)
		bOK = !bOK;
#endif
	if (bOK)
	{
		m_ctldtBegin.GetTime(m_dtBegin);
		COleDateTime newDate = pDTChange->st;
		if (newDate < m_dtBegin)
		{
			MessageBox(_T("结束时间不能小于开始时间"));
			m_ctldtEnd.SetTime(m_dtEnd);
		}
		UpdateData();
	}


	*pResult = 0;
}
